package com.asktgapp.user.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.asktgapp.JGTool.Utils;
import com.asktgapp.R;
import com.asktgapp.application.ApiUtil;
import com.asktgapp.application.Apiservice;
import com.asktgapp.architecture.retrofit.ApiException;
import com.asktgapp.architecture.retrofit.ApiResponseBody;
import com.asktgapp.architecture.rxjava.ExceptionHandle;
import com.asktgapp.base.BaseFragment;
import com.asktgapp.base.SearchBaseActivity;
import com.asktgapp.model.DeviceTypeVO;
import com.asktgapp.model.SecondhandTypeVO;
import com.asktgapp.model.UsedMachineVO;
import com.asktgapp.modulebase.common.adapter.BaseAdapter;
import com.asktgapp.modulebase.common.adapter.BaseViewHolder;
import com.asktgapp.modulebase.common.util.PreferencesUtil;
import com.asktgapp.user.activity.RentMachineActivity;
import com.asktgapp.user.activity.UsedMachineActivity;
import com.asktgapp.user.activity.UsedMachineDetailActivity;
import com.asktgapp.utils.ImageDisplayUtil;
import com.asktgapp.widget.RefreshRecyclerView;

import java.text.DecimalFormat;
import java.util.HashMap;
import java.util.List;

import butterknife.ButterKnife;
import butterknife.InjectView;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;


public class UsenSecondhandAllTypeFragment extends BaseFragment {

    @InjectView(R.id.recycle)
    RefreshRecyclerView refreshRecyclerView;
    private BaseAdapter mTypeAdapter;
    RecyclerView rvSecondhandType;
    private int type = 1;
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        ViewGroup viewGroup = (ViewGroup) inflater.inflate(R.layout.freshrecyclerview, container, false);
        ButterKnife.inject(this, viewGroup);
//        EventBus.getDefault().register(this);
        return super.onCreateView(inflater, viewGroup, savedInstanceState);
    }

    @Override
    protected void initView(View rootView) {
        setHasOptionsMenu(true);
        getBaseActivity().setBackNavigationIcon();
        type = getBaseActivity().getIntent().getIntExtra("type",1);
        refreshRecyclerView.setRefreshAction(new RefreshRecyclerView.Action() {
            @Override
            public void onAction(int page) {
//                mPage = page;
                getMachineAllType();
            }
        });

        mTypeAdapter = new BaseAdapter(getActivity()) {
            @Override
            public BaseViewHolder getViewHolder(ViewGroup parent, int viewType) {
                BaseViewHolder holder = new BaseViewHolder(LayoutInflater.from(getActivity()).inflate(R.layout.list_all_secondhand_type, parent, false), new BaseViewHolder.Callbacks() {
                    @Override
                    public void bind(BaseViewHolder holder, Object data) {
                        if (data instanceof DeviceTypeVO) {
                            DeviceTypeVO bean = (DeviceTypeVO) data;
                            TextView type = holder.getView(R.id.tv_name);
                            type.setText(bean.getName());

                        }

                    }

                    @Override
                    public void onItemClick(View view, int position) {
                        DeviceTypeVO bean = (DeviceTypeVO) mTypeAdapter.getData().get(position);
                        Intent intent;
                        if(type==0)//0 二手设备 1 设备出租
                            intent = new Intent(getActivity(), UsedMachineActivity.class);
                        else
                            intent = new Intent(getActivity(), RentMachineActivity.class);
                        intent.putExtra("typeId", bean.getId());
                        intent.putExtra("typeName", bean.getName());
                        startActivity(intent);
                    }
                });
                return holder;
            }
        };
        rvSecondhandType =  refreshRecyclerView.getRecyclerView();
        rvSecondhandType.setLayoutManager(new LinearLayoutManager(getContext(),LinearLayoutManager.VERTICAL,false));
        rvSecondhandType.setAdapter(mTypeAdapter);
        refreshRecyclerView.setLoadMoreAble(false);
    }


    @Override
    public void loadData() {
        visibleLoading();
        getMachineAllType();
    }


    private void getMachineAllType() {
        Apiservice apiservice = ApiUtil.getInstance().create();
        HashMap params = new HashMap();
        params.put("page", 1);
        params.put("userid", PreferencesUtil.getString(getActivity(), PreferencesUtil.USER_ID));
        Call<ApiResponseBody<List<DeviceTypeVO>>> call = apiservice.getMachineAllType(params);
        call.enqueue(new Callback<ApiResponseBody<List<DeviceTypeVO>>>() {
            @Override
            public void onResponse(Call<ApiResponseBody<List<DeviceTypeVO>>> call, Response<ApiResponseBody<List<DeviceTypeVO>>> response) {
                inVisibleLoading();
                if (refreshRecyclerView == null) {
                    Log.i("JGAPP","refreshRecyclerView == null");
                    return;
                }
                refreshRecyclerView.endPage();
                if (response.isSuccessful()) {
                    List<DeviceTypeVO> deviceTypeVOS = response.body().getResult();
                    if (deviceTypeVOS == null)
                        return;

                        if (deviceTypeVOS.size() == 0) {
                            visibleNoData();
                        } else {
                            mTypeAdapter.setData(deviceTypeVOS);
                        }

                } else {
                    showTost(response.message(), 1);
                }
            }

            @Override
            public void onFailure(Call<ApiResponseBody< List<DeviceTypeVO>>> call, Throwable t) {
                inVisibleLoading();
                ApiException apiException = ExceptionHandle.handleException(t);
                if (apiException.isTokenInvalid()) {
                    showUnLoginSnackbar();
                } else if (apiException.isNetConnected()) {
                    showSetNetworkSnackbar();
                } else {
                    showTost(apiException.getMessage(), 0);
                }
            }
        });


    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
//        EventBus.getDefault().unregister(this);
        ButterKnife.reset(this);

    }


}
